package experiment;

import java.util.Scanner;

/**
 * @Author:云尚
 * @Description:用递归实现斐波那契数列
 * 斐波那契数列：指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...
 */
public class Demo8 {
    public int fibonacci(int num) {
        //通过使用if-else语句来引导不同的情况
        if(num == 0) {
            return 0;
        }else if(num == 1){
            return 1;
        }else{
            return fibonacci(num - 1) + fibonacci(num - 2);
        }
    }

    public static void main(String[] args) {
        Demo8 demo8 = new Demo8();
        Scanner scan = new Scanner(System.in);

        System.out.print("请输入一个整数：");

        int num = scan.nextInt();
        //筛选掉num < 0的情况
        if(num >= 0) {
            System.out.println("斐波那契数列下标为：" + num + "的数为：" + demo8.fibonacci(num));
        }else {
            System.out.println("请输入大于等于0的正整数。");
        }
        System.out.print("下标由0~" + num + "的斐波那契数列为：");
        for(int i = 0;i <= num;i++) {
            System.out.print(demo8.fibonacci(i) + "  ");
        }
    }
}

